<?php

class Admin_Form_Faculdadeprova extends Zend_Form
{

    public function init()
    {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $this->setName('form_'.$controller);
        
        $id = $front->getRequest()->getParam('id', 0);
        $fac_id = $front->getRequest()->getParam('fac_id', 0);
        
        $url .= '/index/fac_id/'.$fac_id;
        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        Zend_Loader::loadClass('formula','library');
        
        $el = new Zend_Form_Element_Hidden('FAP_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('FAP_NOM');
        $el->setLabel('Prova')->setRequired(true);
        $el->setAttrib('required','');
        $this->addElement($el);
        
        $arr = formula::formulainstituicao();
        $el = new Zend_Form_Element_Select('FAP_FORINS',array('onchange' => ''));
        $el->setLabel('Fórmula Classificatória')
        ->addMultiOptions($arr);
        $this->addElement($el);
        
        $html_row = '';
        if($id > 0){
            $query = 'SELECT SQL_CACHE * FROM FACULDADE_PROVA_DISC WHERE FAP_ID = '.$id;
            $result = $db->fetchAll($query);
            foreach($result as $row){
                
                $query = 'SELECT SQL_CACHE * FROM DISCIPLINA WHERE DCP_STA = 1 ORDER BY DCP_NOM';
                $result2 = $db->fetchAll($query);
                $cmb_dis = '<select name="d[]" class="tam_med_g" ><option value="">Nenhum</option>';
                foreach($result2 as $row2){
                    $selected = $row['DCP_ID'] == $row2['DCP_ID'] ? 'selected':'';
                    $cmb_dis .= '<option '.$selected.' value="'.$row2['DCP_ID'].'">'.$row2['DCP_NOM'].'</option>';
                }
                $cmb_dis .= '</select>';
                
                $html_row .= '<div class="cnt_el">';
                $html_row .= 'Disciplina: '.$cmb_dis;
                $html_row .= ' &nbsp; Nº Questões: <input type="text" class="texto numero" name="n[]" value="'.$row['FPD_NQU'].'">';
                $html_row .= ' &nbsp; Peso: <input type="text" class="texto vnum" name="p[]" value="'.$row['FPD_PES'].'">';
                $html_row .= ' &nbsp; <a href="javascript:void(0)" class="excluir">Excluir</a>';
                $html_row .= '</div>';
            }
        }
        
        $query = 'SELECT SQL_CACHE * FROM DISCIPLINA WHERE DCP_STA = 1 ORDER BY DCP_NOM';
        $result = $db->fetchAll($query);
        $cmb_dis = '<select name="d[]" class="tam_med_g" ><option value="">Nenhum</option>';
        foreach($result as $row){
            $cmb_dis .= '<option value="'.$row['DCP_ID'].'">'.$row['DCP_NOM'].'</option>';
        }
        $cmb_dis .= '</select>';
        
        $html2 .= '<div class="cnt_el">';
        $html2 .= 'Disciplina: '.$cmb_dis;
        $html2 .= ' &nbsp; Nº Questões: <input type="text" class="texto" name="n[]" >';
        $html2 .= ' &nbsp; Peso: <input type="text" class="texto" name="p[]" >';
        $html2 .= ' &nbsp; <a href="javascript:void(0)" class="excluir">Excluir</a>';
        $html2 .= '</div>';
        
        $html = '<div id="it_dis">';
        $html .= '<div class="cnt_el"><input type="button" name="add" id="add" value="Adicionar Disciplina" class="btn-frm"></div>';
        $html .= $html_row;
        if($html_row == '') $html .= $html2;
        $html .= '</div>';
        
        $el = new Zend_Form_Element('hrml',array( 'helper'=>'formNote', 'value'=> $html ));
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Checkbox('FAP_STA',array('checked'=>'checked'));
        $el->setLabel('Status');
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Submit('submit',array('disableLoadDefaultDecorators' =>true));
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setLabel('Salvar');
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Voltar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-frm">Voltar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
    }


}

